﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MINERP.DAL;
using MINERP.COM;

namespace MINERP.InfoConfig
{
    public partial class FrmComChargeItem : FrmMDIChildFormTemplate
    {

        private CommonChargeItem _comChargeItem = null;
        private static FrmComChargeItem instance = null;

        public static FrmComChargeItem Instance
        {
            get 
            {
                if (instance == null)
                    instance = new FrmComChargeItem();
                return instance;
            }
            
        }


        private FrmComChargeItem()
        {
            InitializeComponent();
        }

        private void FrmComChargeItem_Load(object sender, EventArgs e)
        {
            LinkCostType();
            LinkComChargeItem();
        }

        private void FrmComChargeItem_FormClosing(object sender, FormClosingEventArgs e)
        {
            instance = null;
        }

        /// <summary>
        /// 获得公共计价项目列表
        /// </summary>
        /// <returns></returns>
        private List<CommonChargeItem> GetComChargeItem()
        {
            using (newSampleYPDBEntities entity = new newSampleYPDBEntities())
            {
                try
                {
                    return entity.CommonChargeItem.ToList();
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    return null;
                }
            }
        }

        /// <summary>
        /// 添加公共计价项目列表
        /// </summary>
        /// <returns>0成功，-1 访问数据库 失败，-2 计价项目名称重复</returns>
        private int AddComChargeItem()
        {
            int ret = 0;

            using (newSampleYPDBEntities entity = new newSampleYPDBEntities())
            {
                try
                {
                    CommonChargeItem comChargeItemInDB = entity.CommonChargeItem.FirstOrDefault(c => c.ItemName == txtComChargeItem.Text.Trim());
                    if (comChargeItemInDB == null)
                    {
                        CommonChargeItem comChargeItem = new CommonChargeItem();
                        
                        comChargeItem.ItemName = txtComChargeItem.Text;
                        comChargeItem.tb_CostType = entity.tb_CostType.FirstOrDefault(c => c.TypeName == cmbCostType.Text);
                        comChargeItem.SinglePrice = txtSinglePrice.Text.Trim() == string.Empty ? 0 : Convert.ToDecimal(txtSinglePrice.Text.Trim());
                        comChargeItem.UnitPrice = txtUnitPrice.Text.Trim() == string.Empty ? 0 : Convert.ToDecimal(txtUnitPrice.Text.Trim());
                        comChargeItem.AreaPrice = txtAreaPrice.Text.Trim() == string.Empty ? 0 : Convert.ToDecimal(txtAreaPrice.Text.Trim());
                        entity.AddToCommonChargeItem(comChargeItem);
                        entity.SaveChanges();
                    }
                    else
                    {
                        ret = -2;
                    }
                }
                catch
                {
                    ret = -1;
                }
            }
            return ret;
            
        }

        /// <summary>
        /// 数据检查
        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            if (string.IsNullOrEmpty(txtComChargeItem.Text.Trim()))
            {
                MessageBox.Show("请输入计价项目名称","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtComChargeItem.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(cmbCostType.Text.Trim()))
            {
                MessageBox.Show("请选择计价类型", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }

        /// <summary>
        /// 绑定计价类型
        /// </summary>
        private void LinkCostType()
        {
            List<tb_CostType> list = new List<tb_CostType>();
            using (newSampleYPDBEntities entity = new newSampleYPDBEntities())
            {
                list = entity.tb_CostType.ToList();
            }
            cmbCostType.Properties.Items.Clear();
            foreach (var item in list)
            {
                cmbCostType.Properties.Items.Add(item.TypeName); 
            }
            cmbCostType.SelectedIndex = 0;
        }

        /// <summary>
        /// 绑定公共计价项目
        /// </summary>
        private void LinkComChargeItem()
        {
            
            using (newSampleYPDBEntities entity = new newSampleYPDBEntities())
            {
                try
                {
                    gcComChargeItem.DataSource = entity.CommonChargeItem.Include("tb_CostType").ToList();
                    gcComChargeItem.RefreshDataSource();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }


        }

        private void gvComChargeItem_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            _comChargeItem = (CommonChargeItem)gvComChargeItem.GetFocusedRow();
            if (_comChargeItem != null)
            {
                txtComChargeItem.Text = _comChargeItem.ItemName;
                cmbCostType.Text = _comChargeItem.tb_CostType.TypeName;
                txtAreaPrice.Text = _comChargeItem.AreaPrice.ToString();
                txtSinglePrice.Text = _comChargeItem.SinglePrice.ToString();
                txtUnitPrice.Text = _comChargeItem.UnitPrice.ToString();
            }
        }
        
        //更新
        private void btnAdd_Click(object sender, EventArgs e)
        {
            int ret =0;
             if (btnAdd.Text != "保存")
            {
                SetContolsStatus(true, DBAccessType.add);
                txtComChargeItem.Focus();
                return;
            }

             if (CheckData())
             {
                 ret = AddComChargeItem();
                 if (ret == 0)
                 {
                     SetContolsStatus(false, DBAccessType.add);
                     LinkComChargeItem();
                     MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                 }
                 else if (ret == -1)
                 {
                     MessageBox.Show("与数据库服务器连接失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                 }
                 else
                 {
                     MessageBox.Show("该计价项目名称已存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                 }
             }
        }

        /// <summary>
        /// 控件设置
        /// </summary>
        /// <param name="bl"></param>
        private void SetContolsStatus(bool bl, DBAccessType optype)
        {
            txtComChargeItem.Enabled = bl;
            cmbCostType.Enabled = bl;
            txtUnitPrice.Enabled = bl;
            txtSinglePrice.Enabled = bl;
            txtAreaPrice.Enabled = bl;
            switch ((int)optype)
            {
                case 0:
                    txtComChargeItem.Text = string.Empty;
                    if (bl)
                    {
                        btnAdd.Text = "保存";
                        btnUpdate.Enabled = false;
                        btnDelete.Enabled = false;
                    }
                    else
                    {
                        btnAdd.Text = "添加";
                        btnUpdate.Enabled = true;
                        btnDelete.Enabled = true;
                    }
                    break;

                case 1:

                    if (bl)
                    {
                        btnUpdate.Text = "保存";
                        btnAdd.Enabled = false;
                        btnDelete.Enabled = false;
                    }
                    else
                    {
                        btnUpdate.Text = "更新";
                        btnAdd.Enabled = true;
                        btnDelete.Enabled = true;
                    }
                    break;
                case 2:
                    btnAdd.Text = "添加";
                    btnUpdate.Text = "更新";
                    btnAdd.Enabled = true;
                    btnUpdate.Enabled = true;
                    btnDelete.Enabled = true;
                    break;
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            SetContolsStatus(false, DBAccessType.delete);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (btnUpdate.Text != "保存")
            {
                SetContolsStatus(true, DBAccessType.update);
                txtComChargeItem.SelectAll();
                return;
            }
            if (CheckData())
            {
                UpdateComChargeItem();

            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <returns></returns>
        private void UpdateComChargeItem()
        {
            using (newSampleYPDBEntities entity = new newSampleYPDBEntities())
            {
                try
                {
                    CommonChargeItem comChargeItem = entity.CommonChargeItem.FirstOrDefault(c => c.ItemName == _comChargeItem.ItemName);
                    CommonChargeItem comChargeItemInDB = entity.CommonChargeItem.FirstOrDefault(c => c.ItemName == txtComChargeItem.Text.Trim());
                    if (comChargeItem != null)
                    {
                        if (comChargeItemInDB != null)
                        {
                            if (comChargeItem.CommonChargeItemId != comChargeItemInDB.CommonChargeItemId)
                            {
                                MessageBox.Show("该计价项目已存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                txtComChargeItem.SelectAll();
                                return;
                            }
                        }
                        comChargeItem.ItemName = txtComChargeItem.Text.Trim();
                        comChargeItem.tb_CostType = entity.tb_CostType.First(t => t.TypeName == cmbCostType.Text.Trim());
                        comChargeItem.SinglePrice = txtSinglePrice.Text.Trim() == string.Empty ? 0 : Convert.ToDecimal(txtSinglePrice.Text.Trim());
                        comChargeItem.UnitPrice = txtUnitPrice.Text.Trim() == string.Empty ? 0 : Convert.ToDecimal(txtUnitPrice.Text.Trim());
                        comChargeItem.AreaPrice = txtAreaPrice.Text.Trim() == string.Empty ? 0 : Convert.ToDecimal(txtAreaPrice.Text.Trim());
                        entity.SaveChanges();
                        LinkComChargeItem();
                        SetContolsStatus(false, DBAccessType.update);
                        MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("请选择计价项目", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
        //删除
        private void btnDelete_Click(object sender, EventArgs e)
        {
            Delete();
        }
        /// <summary>
        /// 删除
        /// </summary>
        private void Delete()
        {
            using (newSampleYPDBEntities entity = new newSampleYPDBEntities())
            {
                try
                {
                    if (_comChargeItem != null)
                    {
                        CommonChargeItem comChargeItem = entity.CommonChargeItem.FirstOrDefault(c => c.ItemName == _comChargeItem.ItemName);
                        entity.DeleteObject(comChargeItem);
                        entity.SaveChanges();
                        LinkComChargeItem();
                        MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("请选择计价项目", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
    }
}
